package com.wang.luntan.common.web.controller;

import com.wang.luntan.common.util.VerifyCodeUtils;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.IOException;

/**
 * 公共的方法
 *
 * @author WangshSxt
 */
@Log4j2
@Controller
@RequestMapping("/common/")
public class CommonController extends BaseController {
    /**
     * 生成验证码
     *
     * @return
     * @throws IOException
     */
    @RequestMapping("/randImg")
    public void randImg(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setContentType("image/jpeg");

        //生成随机字串
        String verifyCode = VerifyCodeUtils.generateVerifyCode(4);
        //存入会话session
        HttpSession session = request.getSession(true);
        session.setAttribute("randSess", verifyCode);
        //生成图片
        int w = 200, h = 80;
        VerifyCodeUtils.outputImage(w, h, response.getOutputStream(), verifyCode);
    }

    /**
     * 生成验证码
     *
     * @return
     * @throws IOException
     */
    @RequestMapping("/info")
    public String info(HttpServletRequest request, HttpServletResponse response) throws IOException {
        log.info("==info==");
        return "/info";
    }
}
